#include "BitEncodedSolution.h"

BitEncodedSolution::BitEncodedSolution() 
{
}

BitEncodedSolution::BitEncodedSolution(vector<bool> solution) 
{
    this->solution = solution;
}

vector<bool> BitEncodedSolution::getSolution() 
{
    return solution;
}

void BitEncodedSolution::setSolution(vector<bool> solution) 
{
    this->solution = solution;
}

Solution* BitEncodedSolution::clone() 
{
    BitEncodedSolution* clone = new BitEncodedSolution();
    clone->setFitness(fitness);
    clone->setSolution(solution);
    return clone;
}

//TODO: this mutate function is probably really inefficient.
//is there anything that can be done to make it smarter?
void BitEncodedSolution::mutate() {
    for (unsigned int x = 0; x < solution.size(); x++) 
	{
        //if we randomly determine to mutate on this bit...
//        if ((rand() % 1000000) < (mutationRate * 1000000)) 
        if ((MyRandom::randDbl() * 1000000) < (mutationRate * 1000000)) 
        {
            //...invert the bit
            solution[x] = !solution[x]; 
        }
    }
}


/** Fills this solution with 'size' entropy bits */
void BitEncodedSolution::fillWithRandomBits(unsigned int size) {
    for (unsigned int x = 0; x < size; x++) {
        addBit((bool) MyRandom::randInt(0, 1));
    }
}

ostream& BitEncodedSolution::outputSolution(ostream &output) 
{
    for (unsigned int x = 0; x < solution.size(); x++) 
    {
        output << solution[x];
    }
    
    return output;
}

unsigned int BitEncodedSolution::getSize() 
{ 
    return solution.size(); 
}

void BitEncodedSolution::setBit(unsigned int x, bool newVal) 
{ 
    solution[x] = newVal; 
}

bool BitEncodedSolution::getBit(unsigned int x) 
{ 
    return solution[x]; 
}
void BitEncodedSolution::addBit(bool value) 
{ 
    solution.push_back(value); 
}

SolutionType BitEncodedSolution::getSerializationIdentifyer() {
    return SF_BIT_ENCODED_SOLUTION;
}
